import Vue from "vue";
import VueRouter from "vue-router";
import i18n from "@/js/i18n";

// 避免路由重复
const originalPush = VueRouter.prototype.push;
// 修改 原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Login",
    component: () => import("../views/LoginView.vue"),
    meta: {
      title: "title.login",
      pure: true,
    },
  },
  {
    path: "/dashboard",
    component: () => import("@/views/DashboardView.vue"),
    children: [
      {
        path: "",
        name: "Dashboard",
        component: () => import("@/views/HomeView.vue"),
        meta: {
          title: "title.dashboard",
        },
      },
      {
        path: "/Course",
        name: "Course",
        component: () => import("@/views/course/VodView.vue"),

        meta: {
          title: "title.course.vod.index",
        },
        children: [
          {
            path: "/Vod",
            name: "Vod",
            component: () => import("@/views/course/vod/index"),
            meta: {
              title: "title.course.vod.index",
              keepAlive: true,
            },
          },
          {
            path: "/course/vod/video-import",
            component: () => import("@/views/course/vod/video/import.vue"),
            name: "VodImport",
            meta: {
              title: "title.course.vod.video-import",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/create",
            component: () => import("@/views/course/vod/create"),
            name: "VodCreate",
            meta: {
              title: "title.course.vod.create",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/update",
            component: () => import("@/views/course/vod/update"),
            name: "VodUpdate",
            meta: {
              title: "title.course.vod.update",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/:id/view",
            component: () => import("@/views/course/vod/detail"),
            name: "VodView",
            meta: {
              title: "title.course.vod.detail",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/components/vod-comments",
            component: () => import("@/views/course/vod/components/vod-comments"),
            name: "CourseComments",
            meta: {
              title: "title.course.vod.comment",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/category/index",
            component: () => import("@/views/course/vod/category/index"),
            name: "CourseCategories",
            meta: {
              title: "title.course.vod.category.index",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/category/create",
            component: () => import("@/views/course/vod/category/create"),
            name: "CategoriesCreate",
            meta: {
              title: "title.course.vod.category.create",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/category/update",
            component: () => import("@/views/course/vod/category/update"),
            name: "CategoriesUpdate",
            meta: {
              title: "title.course.vod.category.update",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/chapter/index",
            component: () => import("@/views/course/vod/chapter/index"),
            name: "CourseChapters",
            meta: {
              title: "title.course.vod.chapter.index",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/chapter/create",
            component: () => import("@/views/course/vod/chapter/create"),
            name: "ChaptersCreate",
            meta: {
              title: "title.course.vod.chapter.create",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/chapter/update",
            component: () => import("@/views/course/vod/chapter/update"),
            name: "ChaptersUpdate",
            meta: {
              title: "title.course.vod.chapter.update",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/attach/index",
            component: () => import("@/views/course/vod/attach/index"),
            name: "CourseAttach",
            meta: {
              title: "title.course.vod.attach.index",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/attach/create",
            component: () => import("@/views/course/vod/attach/create"),
            name: "AttachCreate",
            meta: {
              title: "title.course.vod.attach.create",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/index",
            component: () => import("@/views/course/vod/video/index"),
            name: "CourseVideos",
            meta: {
              title: "title.course.vod.video.index",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/create",
            component: () => import("@/views/course/vod/video/create"),
            name: "VideosCreate",
            meta: {
              title: "title.course.vod.video.create",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/update",
            component: () => import("@/views/course/vod/video/update"),
            name: "VideosUpdate",
            meta: {
              title: "title.course.vod.video.update",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/subscribe",
            component: () => import("@/views/course/vod/video/subscribe"),
            name: "VideoSubscribe",
            meta: {
              title: "title.course.vod.video.subscribe",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/watch-records",
            component: () => import("@/views/course/vod/video/watch-records"),
            name: "VideoWatchRecords",
            meta: {
              title: "title.course.vod.video.watch-records",
              is_defaultActive: "Vod",
            },
          },
          {
            path: "/course/vod/video/comments",
            component: () => import("@/views/course/vod/video/comments"),
            name: "VideoComments",
            meta: {
              title: "title.course.vod.video.comments",
              is_defaultActive: "Vod",
            },
          },
        ],
      },
      {
        path: "/MemberIndex",
        name: "",
        component: () => import("@/views/MemberView.vue"),

        meta: {
          title: "title.member.index",
          keepAlive: true,
        },
        children: [
          {
            path: "",
            name: "MemberIndex",
            component: () => import("@/views/Member/MemberIndex.vue"),
            meta: {
              title: "title.member.index",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/create",
            name: "MemberCreate",
            component: () => import("@/views/Member/MemberCreate.vue"),
            meta: {
              title: "添加学员",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/import",
            name: "MemberImport",
            component: () => import("@/views/Member/MemberImport.vue"),
            meta: {
              title: "学员批量导入",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/:id/edit",
            name: "MemberEdit",
            component: () => import("@/views/Member/MemberEdit.vue"),
            meta: {
              title: "编辑学员",
              is_defaultActive: "MemberIndex",
            },
          },
          
          {
            path: "member/profile/:id",
            name: "MemberProfiles",
            component: () => import("@/views/Member/MemberProfiles.vue"),
            meta: {
              title: "实名信息",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/:id/credit1",
            name: "MemberCredit1",
            component: () => import("@/views/Member/MemberCredit1.vue"),
           
            meta: {
              title: "学员积分",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/:id/tags",
            name: "MemberTags",
            component: () => import("@/views/Member/MemberTags.vue"),
            meta: {
              title: "学员标签",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/:id/remark",
            name: "MemberRemark",
            component: () => import("@/views/Member/MemberRemark.vue"),
            meta: {
              title: "学员备注",
              is_defaultActive: "MemberIndex",
            },
          },
          {
            path: "member/:id",
            name: "MemberDetail",
            component: () => import("@/views/Member/MemberDetail.vue"),
            meta: {
              title: "学员详情",
              is_defaultActive: "MemberIndex",
            },
          },
          
        ],
      },

      {
        path: "/change-password",
        name: "AdministratorChangePassword",
        component: () => import("@/views/ChangePassView.vue"),
        meta: {
          title: "title.administrator.change-password",
          is_defaultActive: "Dashboard",
        },
      },
      {
        path: "/Orderlist",
        name: "Orderlist",
        component: () => import("@/views/Financial/OrderListView.vue"),

        meta: {
          title: "title.order",
          keepAlive: true,
        },
      },
      {
        path: "/WithdrawOrders",
        name: "WithdrawOrders",
        component: () => import("@/views/Financial/WithdrawOrders.vue"),
        meta: {
          title: "title.withdrawOrders",
          keepAlive: true,
        },
      },

      // VIP会员
      {
        path: "/Role",
        component: () => import("@/views/operate/Role.vue"),
        children: [
          {
            path: "",
            name: "Role",
            component: () => import("@/views/operate/role/RoleView.vue"),
            meta: {
              title: "title.role",
              keepAlive: true,
              is_defaultActive: "Role",
            },
          },
          {
            path: "/addrole",
            name: "Addrole",
            component: () => import("@/views/operate/role/addRoleView.vue"),
            meta: {
              title: "title.role",
              is_defaultActive: "Role",
            },
          },
          {
            path: "/Editrole",
            name: "Editrole",
            component: () => import("@/views/operate/role/editRoleView.vue"),
            meta: {
              title: "title.role",
              is_defaultActive: "Role",
            },
          },
        ],
      },

      // 优惠码
      {
        path: "/Promocode",
        component: () => import("@/views/operate/promocode.vue"),
        children: [
          {
            path: "",
            name: "Promocode",
            component: () => import("@/views/operate/promocode/PromocodeView.vue"),
            meta: {
              title: "title.promocode",
              keepAlive: true,
              is_defaultActive: "Promocode",
            },
          },
          {
            path: "/createcode",
            name: "Createcode",
            component: () => import("@/views/operate/promocode/CreateCodeView.vue"),
            meta: {
              title: "title.promocodecreate",
              is_defaultActive: "Promocode",
            },
          },
          {
            path: "/order/code-import",
            name: "CodeImport",
            component: () => import("@/views/operate/promocode/CodeImportView.vue"),
            meta: {
              title: "title.promocode_import",
              is_defaultActive: "Promocode",
            },
          },
          {
            path: "/createmulticode",
            name: "CreateMulticode",
            component: () => import("@/views/operate/promocode/CreateMulticode.vue"),
            meta: {
              title: "title.promocode_export",
              is_defaultActive: "Promocode",
            },
          },
        ],
      },

      // 公众号
      {
        path: "/Messagereply",
        component: () => import("@/views/operate/MessageReply.vue"),
        children: [
          {
            path: "",
            name: "MessageReply",
            component: () => import("@/views/operate/MessageReply/MessageReplyView.vue"),
            meta: {
              title: "title.wechat.messagereply.index",
              keepAlive: true,
              is_defaultActive: "MessageReply",
            },
          },
          {
            path: "create",
            name: "ReplyCreate",
            component: () => import("@/views/operate/MessageReply/ReplyCreate.vue"),
            meta: {
              title: "title.wechat.messagereply.create",
              is_defaultActive: "MessageReply",
            },
          },
          {
            path: "/decoration/mp-wechat-menu",
            name: "MpWechatMenu",
            component: () => import("@/views/operate/MessageReply/MpWechatMenu.vue"),
            meta: {
              title: `title.mp-wechat-menu`,
              is_defaultActive: "MessageReply",
            },
          },
          {
            path: "/system/mp_wechatConfig",
            name: "SystemMpwechatConfig",
            component: () => import("@/views/operate/MessageReply/MpwechatConfig.vue"),
            meta: {
              title: "title.system-config",
            },
          },
          {
            path: "update",
            name: "ReplyUpdate",
            component: () => import("@/views/operate/MessageReply/updateView.vue"),
            meta: {
              title: "title.wechat.messagereply.update",
              is_defaultActive: "MessageReply",
            },
          },
        ],
      },
      {
        path: "/SystemAdministrator",
        name: "SystemAdministrator",
        component: () => import("@/views/SystemAdminView.vue"),
        meta: {
          title: "title.system-administrator",
          keepAlive: true,
        },
      },
      {
        path: "/SystemAdministrator/update",
        name: "update",
        component: () => import("@/views/UpdateView.vue"),
        meta: {
          title: "编辑管理员",

          keepAlive: true,
        },
      },
      {
        path: "/SystemAdministrator/create",
        name: "create",
        component: () => import("@/views/CreateView.vue"),
        meta: {
          title: "添加管理员",

          keepAlive: true,
        },
      },
      {
        path: "/SystemAdministrator/adminroles",
        name: "adminroles",
        component: () => import("@/views/AdminrolesView.vue"),
        meta: {
          title: "管理员角色",

          keepAlive: true,
        },
      },
      {
        path: "/SystemAdministrator/adminroles/create",
        name: "adminrolesCreate",
        component: () => import("@/views/AdminrolesCreateView.vue"),
        meta: {
          title: "添加管理员角色",

          keepAlive: true,
        },
      },
      {
        path: "/SystemAdministrator/adminroles/update",
        name: "update",
        component: () => import("@/views/AdminrolesUpdateView.vue"),
        meta: {
          title: "编辑管理员角色",

          keepAlive: true,
        },
      },
    ],
  },

  // 找不到指定页面重定向 404
  {
    path: "/404",
    name: "PageNotFound",
    component: () => import("@/views/PageNotFound.vue"),
    meta: {
      title: "404 Not Found",
    },
  },
  {
    path: "*",
    redirect: "/404",
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // 设置标题
  document.title = i18n.t(to.meta.title);
  const token = localStorage.getItem("admin-token");
  if (token) {
    next();
  } else {
    if (to.name === "Login") {
      next();
    } else {
      next("/");
    }
  }
});

export default router;
